package util;

import java.io.BufferedInputStream;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

public class StringUtils {

	/**
	 * To Title Case
	 * 
	 * @param cs
	 *            CharSequence
	 * @return String
	 */
	public static String toTitleCase(CharSequence cs) {
		int csLength = -1;
		if (cs == null || (csLength = cs.length()) <= 0) {
			return "";
		}
		StringBuffer result = new StringBuffer();
		// length > 0
		result.append(Character.toTitleCase(cs.charAt(0)));
		if (csLength > 1) {
			result.append(cs.subSequence(1, csLength));
		}
		return result.toString();
	}

	/**
	 * Is Any Empty
	 * 
	 * @param strings
	 *            CharSequence
	 * @return boolean
	 */
	public static boolean isAnyEmpty(CharSequence... strings) {
		if (strings == null || strings.length <= 0) {
			return true;
		}
		for (CharSequence str : strings) {
			if (str == null || str.length() <= 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Format String
	 * 
	 * @param str
	 *            String
	 * @param prefix
	 *            String
	 * @param args
	 *            Object...
	 * @return String
	 */
	public static String format(String str, String prefix, Object... args) {
		if (str == null) {
			return null;
		}
		int argsSize = 0;
		if (prefix == null || args == null || str.length() <= 0
				|| prefix.length() <= 0 || (argsSize = args.length) <= 0) {
			return str;
		}
		String[] searchArray = new String[argsSize];
		String[] replacementArray = new String[argsSize];
		for (int i = 0; i < argsSize; i++) {
			int reverseIndex = argsSize - 1 - i;
			searchArray[i] = prefix.concat(String.valueOf(reverseIndex + 1));
			replacementArray[i] = String.valueOf(args[reverseIndex]);
		}
		return org.apache.commons.lang.StringUtils.replaceEach(str,
				searchArray, replacementArray);
	}

	/**
	 * Camel Case To Underline Case
	 * 
	 * @param str
	 *            String
	 * @return String
	 */
	public static String camelCaseToUnderlineCase(String str) {
		if (str == null || str.length() <= 0) {
			return "";
		}
		StringBuilder ret = new StringBuilder();
		char[] charArray = str.toCharArray();
		for (int i = 0, charArraySize = charArray.length; i < charArraySize; i++) {
			char c = charArray[i];
			if (Character.isUpperCase(c)) {
				if (i > 0 && Character.isLowerCase(charArray[i - 1])) {
					ret.append("_");
				}
				c = Character.toLowerCase(c);
			}
			ret.append(c);
		}
		return ret.toString();
	}

	/**
	 * Upper Camel Case To Lower Camel Case
	 * 
	 * @param str
	 *            String
	 * @return String
	 */
	public static String upperCamelCaseToLowerCamelCase(String str) {
		if (str == null || str.length() <= 0) {
			return "";
		}
		StringBuilder ret = new StringBuilder();
		ret.append(str);
		ret.setCharAt(0, Character.toLowerCase(ret.charAt(0)));
		return ret.toString();
	}

	/**
	 * Read File To String
	 * 
	 * @param filePath
	 *            String
	 * @return String
	 */
	public static String readFileToString(String filePath) {
		String data = null;
		InputStream is = null;
		try {
			is = new BufferedInputStream(
					StringUtils.class.getResourceAsStream("/".concat(filePath)));
			data = IOUtils.toString(is);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeQuietly(is);
		}
		return data;
	}

	/**
	 * To Class Full Name
	 * 
	 * @param packageName
	 *            String
	 * @param className
	 *            String
	 * @return String
	 */
	public static String toClassFullName(String packageName, String className) {
		if (className == null || className.length() <= 0) {
			return "";
		}
		StringBuilder ret = new StringBuilder();
		if (packageName != null && packageName.length() > 0) {
			ret.append(packageName);
			ret.append(".");
		}
		ret.append(className);
		return ret.toString();
	}

}